package com.java110.mall.shop.bmo.shopVipUser.impl;

import com.java110.dto.coupon.CouponUserDto;
import com.java110.dto.vipUser.ShopVipAccountDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.intf.acct.ICouponUserV1InnerServiceSMO;
import com.java110.intf.acct.IShopVipAccountInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.mall.shop.bmo.shopVipUser.IGetShopVipUserBMO;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getShopVipUserBMOImpl")
public class GetShopVipUserBMOImpl implements IGetShopVipUserBMO {

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;

    @Autowired
    private IShopVipAccountInnerServiceSMO shopVipAccountInnerServiceSMOImpl;
    @Autowired
    private ICouponUserV1InnerServiceSMO couponUserV1InnerServiceSMOImpl;

    /**
     * @param shopVipUserDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ShopVipUserDto shopVipUserDto) {


        int count = shopVipUserInnerServiceSMOImpl.queryShopVipUsersCount(shopVipUserDto);

        List<ShopVipUserDto> shopVipUserDtos = null;
        if (count > 0) {
            shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        } else {
            shopVipUserDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopVipUserDto.getRow()), count, shopVipUserDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * 搜素会员信息
     *
     * @param shopVipUserDto
     * @return
     */
    @Override
    public ResponseEntity<String> search(ShopVipUserDto shopVipUserDto) {
        List<ShopVipUserDto> shopVipUserDtos = null;
        //用电话号查询
        if (shopVipUserDto.getVipTel().length() == 11) {
            shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        }
        if (shopVipUserDtos != null && shopVipUserDtos.size() > 0) {
            coupUser(shopVipUserDtos);
            refreshAccount(shopVipUserDtos.get(0));
            return ResultVo.createResponseEntity(shopVipUserDtos.get(0));
        }

        //根据名称查询
        shopVipUserDto.setVipNameLike(shopVipUserDto.getVipTel());
        shopVipUserDto.setVipTel("");
        shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        if (shopVipUserDtos != null && shopVipUserDtos.size() > 0) {
            coupUser(shopVipUserDtos);
            refreshAccount(shopVipUserDtos.get(0));
            return ResultVo.createResponseEntity(shopVipUserDtos.get(0));
        }



        return ResultVo.error("未查询到会员");
    }

    private void coupUser(List<ShopVipUserDto> shopVipUserDtos) {
        CouponUserDto couponUserDto = new CouponUserDto();
        couponUserDto.setTel(shopVipUserDtos.get(0).getVipTel());
        List<CouponUserDto> couponUserDtos = couponUserV1InnerServiceSMOImpl.queryCouponUsers(couponUserDto);
        shopVipUserDtos.get(0).setCouponUserDtos(couponUserDtos);
        shopVipUserDtos.get(0).setCouponCount(couponUserDtos.size()+"");
    }

    private void refreshAccount(ShopVipUserDto shopVipUserDto) {

        ShopVipAccountDto shopVipAccountDto = new ShopVipAccountDto();
        shopVipAccountDto.setAcctType(ShopVipAccountDto.ACCT_TYPE_INTEGRAL);
        shopVipAccountDto.setVipId(shopVipUserDto.getVipId());
        shopVipAccountDto.setShopId(shopVipUserDto.getShopId());
        List<ShopVipAccountDto> shopVipAccountDtos = shopVipAccountInnerServiceSMOImpl.queryShopVipAccounts(shopVipAccountDto);

        if (shopVipAccountDtos == null || shopVipAccountDtos.size() < 1) {
            shopVipUserDto.setIntegralAmount("0");
            return;
        }
        shopVipUserDto.setIntegralAmount(shopVipAccountDtos.get(0).getAmount());
    }

}
